package com.cdd.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsMapper;
import com.cdd.goods.service.IGoodsService;

import com.common.entity.Goods;
import com.common.entity.GoodsBrand;
import com.common.entity.GoodsSpecification;
import com.common.untils.CommonResult;
import com.common.vo.GoodsVo;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-01
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, Map map) {
        Page<GoodsVo> page = new Page<>(currentPage,pageSize);
        QueryWrapper<GoodsVo> wrapper=new QueryWrapper();
        wrapper.eq("g.is_deleted",0);
        Object id = map.get("id");
        if (id!=null){
            wrapper.eq("type_id",id);
        }
        Object gname = map.get("gname");
        if (gname!=null){
            wrapper.like("gname",gname);
        }
        Page<GoodsVo> mapPage = goodsMapper.selectGoodsAll(page, wrapper);
        return new CommonResult(2000,"查询成功",mapPage);
    }

    @Override
    public CommonResult deletegoods(String id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        Goods goods=new Goods();
        goods.setIsDeleted(1);
        int update = goodsMapper.update(goods, wrapper);
        System.out.println(update);
        return new CommonResult(2000, "删除成功", null);
    }

    @Override
    public CommonResult insert(GoodsVo goodsVo) {
        //System.out.println(goodsVo+"++++++++++++++++++++");
        goodsVo.setIsDeleted(0);
        List<Goods> goods = goodsMapper.selectGname();
        System.out.println(goods+"0000000000000000000");
        for (Goods g:goods) {
            System.out.println(g+"9999999999999999");
            if (g.getGname().equals(goodsVo.getGname()) && g.getGbid().equals(goodsVo.getGbid())){
                return new CommonResult(5000, "添加失败，已有该商品", null);
            }
            if (g.getCode().equals(goodsVo.getCode())){
                return new CommonResult(5000, "添加失败，已有该商品编号", null);
            }
        }
        goodsMapper.insert(goodsVo);
        return new CommonResult(2000, "添加成功", null);
    }

    @Override
    public CommonResult update(Goods goods) {
        QueryWrapper<Goods> wrapper=new QueryWrapper<>();
        if(goods.getTypeId()!=null){
           goods.setTypeId(goods.getTypeId());
        }
        if (StringUtils.hasText(goods.getGname())) {
           goods.setGname(goods.getGname());
        }
        if (goods.getImg()!=null){
            goods.setImg(goods.getImg());
        }
        if (StringUtils.hasText(goods.getDescription())) {
            goods.setDescription(goods.getDescription());
        }
        if (goods.getGsid()!=null){
            goods.setGsid(goods.getGsid());
        }
        if(goods.getGbid()!=null){
            goods.setGbid(goods.getGbid());
        }
        if(goods.getCode()!=null){
            goods.setCode(goods.getCode());
        }
       wrapper.eq("id",goods.getId());
        int update = goodsMapper.update(goods, wrapper);
        return new CommonResult(2000, "修改成功", null);
    }

    @Override
    public  List<GoodsVo> selectGoodsZone() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("g.is_deleted",0);
        queryWrapper.eq("gs.state",0);
        List<GoodsVo> list=goodsMapper.selectGoodsZone(queryWrapper);
        return list;
    }

    @Override
    public CommonResult findAll(Integer currentPage,Integer pageSize) {
        Page<Goods> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("g.is_deleted",0);
        IPage<Map> page1 = goodsMapper.selectGoods(page,queryWrapper);
        return new CommonResult(2000,"查询出成功",page1);
    }

    @Override
    public CommonResult collect(Integer zid, Integer sid,String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String cid = client.getString("id");
        System.out.println(cid);
        Integer row = goodsMapper.collectGoods(zid,sid,cid);
        if(row==1){
            return new CommonResult(2000,"收藏成功",null);
        }else {
            return new CommonResult(5000,"收藏失败",null);
        }
    }

    @Override
    public CommonResult qxcollect(Integer zid, Integer sid) {
        Integer row = goodsMapper.updateDeleted(zid,sid);
        if(row==1){
            return new CommonResult(2000,"取消收藏成功",null);
        }else {
            return new CommonResult(5000,"取消收藏失败",null);
        }
    }

    @Override
    public CommonResult findAllCollect(com.cdd.goods.vo.GoodsVo goodsVo,String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String cid = client.getString("id");
        Page page = new Page(goodsVo.getCurrentPage(),goodsVo.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.hasText(goodsVo.getGoodsname())){
            queryWrapper.like("gname",goodsVo.getGoodsname());
        }
        if(StringUtils.hasText(goodsVo.getBeginTime())){
            queryWrapper.ge("s.gmt_create",goodsVo.getBeginTime());
        }
        if(StringUtils.hasText(goodsVo.getEndTime())){
            queryWrapper.le("s.gmt_create",goodsVo.getEndTime());
        }
        queryWrapper.eq("s.is_deleted",0);
        queryWrapper.eq("s.cid",cid);
        IPage<Map> iPage = goodsMapper.findAllCollect(page,queryWrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

    @Override
    public CommonResult findByShopIdCollect(String shopid, String goodsid, String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String cid = client.getString("id");
        Map byShopIdCollect = goodsMapper.findByShopIdCollect(cid, shopid, goodsid);
        if (byShopIdCollect!=null){
            return new CommonResult(2000,"已收藏",0);
        }
        return new CommonResult(2000,"未收藏",1);
    }


}
